package com.mi.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.support.Parameter;
import com.mi.entity.MyPages;
import com.mi.entity.NewsEntity;
import com.mi.entity.ResponseMsg;
import com.mi.service.NewsService;
import com.mi.util.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class NewsController {

    @Reference(check = false)
    NewsService newsService;

    //新闻列表
    @RequestMapping("newsList")
    public String newsList(HttpServletRequest request,NewsEntity newsEntity){
        MyPages<NewsEntity> newsEntityMyPages = newsService.queryNewss(newsEntity);
        request.setAttribute("pageInfo",newsEntityMyPages);
        request.setAttribute("n_name",newsEntity.getN_name());
        request.setAttribute("n_issuer",newsEntity.getN_issuer());
        request.setAttribute("n_category",newsEntity.getN_category());
        return "newsList";
    }

    //新闻详情
    @RequestMapping("newsDetails")
    public String newDetails(HttpServletRequest request,NewsEntity newsEntity){
        MyPages<NewsEntity> newsEntityMyPages = newsService.queryNewss(newsEntity);
        if(newsEntityMyPages.getList()!=null && newsEntityMyPages.getList().size()>0){
            newsEntityMyPages.getList().get(0).setN_desc( newsEntityMyPages.getList().get(0).getN_desc().replace("/admin/",""));
            newsEntityMyPages.getList().get(0).setN_image( newsEntityMyPages.getList().get(0).getN_image().replace("/admin/",""));
            request.setAttribute("news",newsEntityMyPages.getList().get(0));
        }
        return "newsDetails";
    }

    //跳转到添加界面
    @RequestMapping("newsAdd")
    public String newsAdd(){
        return "newsAdd";
    }

    //添加新闻
    @RequestMapping("addNews")
    @ResponseBody
    public ResponseMsg addNews(MultipartFile myfile, NewsEntity newsEntity,String descc, HttpServletRequest request){
        ResponseMsg responseMsg = new ResponseMsg();
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String customerImage = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"userIcon";
            String newsDesc = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"news";
            File file = new File(customerImage);
            if(!file.exists()){
                file.mkdirs();
            }
            file = new File(newsDesc);
            if(!file.exists()){
                file.mkdirs();
            }
            customerImage = customerImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            newsDesc = newsDesc + File.separator+ UUIDUtil.getUUID()+".html";
            try {
                FileOutputStream fos = new FileOutputStream(customerImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos = new FileOutputStream(newsDesc);
                FileCopyUtils.copy(descc.getBytes(),fos);
                fos.close();
                is.close();
                newsEntity.setN_image(File.separator+"admin"+File.separator+customerImage.substring(customerImage.lastIndexOf(File.separator,customerImage.lastIndexOf(File.separator)-1)+1));
                newsEntity.setN_desc(File.separator+"admin"+File.separator+newsDesc.substring(newsDesc.lastIndexOf(File.separator,newsDesc.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(newsEntity.getN_category()==0){
            newsEntity.setN_kindName("公司新闻");
        }else if(newsEntity.getN_category()==1){
            newsEntity.setN_kindName("行业新闻");
        }else{
            newsEntity.setN_kindName("学术新闻");
        }
        boolean b = newsService.addNews(newsEntity);
        if (b) {
            responseMsg.setResult("0000");
        } else {
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("添加失败");
        }
        return responseMsg;
    }

    //删除新闻
    @RequestMapping("delNews")
    @ResponseBody
    public ResponseMsg delNews(String ids){
        ResponseMsg responseMsg = new ResponseMsg();
        if(ids == null||"".equals(ids)){
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("删除失败");
        }else{
            String[] nid;
            if (ids.contains(",")) {
                nid = ids.split(",");
            } else {
                nid = new String[]{ids};
            }
            boolean b = newsService.delNews(nid);
            if (b) {
                responseMsg.setResult("0000");
            } else {
                responseMsg.setResult("0001");
                responseMsg.setResultMsg("删除失败");
            }
        }
        return responseMsg;
    }

    //修改新闻状态
    @RequestMapping("updNewsStatus")
    @ResponseBody
    public ResponseMsg updNewsStatus(NewsEntity newsEntity){
        ResponseMsg responseMsg = new ResponseMsg();
        boolean b = false;
        try {
            b = newsService.updNewsStatus(newsEntity);
            if(b){
                responseMsg.setResult("0000");
            }else{
                responseMsg.setResult("0001");
                responseMsg.setResultMsg("修改失败");
            }
        } catch (Exception e) {
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("修改失败");
        }finally {
            return responseMsg;
        }
    }

}
